<template>
	<xn-form-container
		:title="formData.id ? '编辑收支结算' : '增加收支结算'"
		:width="700"
		v-model:open="open"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-row :gutter="16">
				<a-col :span="12" v-show="formData.id">
					<a-form-item label="结算时间：" name="createTime">
						{{ formData.createTime }}
					</a-form-item>
				</a-col>
				<a-col :span="12" v-show="formData.id">
					<a-form-item label="结算单号：" name="businessCode">
						{{ formData.businessCode }}
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="门店：" name="businessShippingStoreId">
						<xn-page-select
							ref="xnbusinessShippingStoreIdPageSelectRef"
							v-model:value="formData.businessShippingStoreId"
							placeholder="请选择门店"
							allow-clear
							show-search
							:page-function="businessShippingStoreIdSelector"
							:echo-function="businessShippingStoreIdEcho"
							@change="settlementStoreChange"
							:disabled="formData.businessStatus==='1' || formData.businessStatus==='2'"
						/>
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-form-item label="结算类型：" name="businessSettlementType">
						<a-radio-group v-model:value="formData.businessSettlementType"
									   placeholder="请选择结算类型" :options="businessSettlementTypeOptions"
									   @change="businessSettlementTypeChange"
									   :disabled="formData.businessStatus==='1' || formData.businessStatus==='2'"/>
					</a-form-item>
				</a-col>
				<a-col :span="6" v-show="formData.businessProjectIdCode === '01' || formData.businessProjectIdCode === '02' || formData.businessProjectIdCode === '05' || formData.businessProjectIdCode === '06'">
					<a-form-item label=" " >
						<a-checkbox v-model:checked="isSettlementByTicket" @change="showBusinessIds" >按单结算</a-checkbox>
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="结算项目：" name="businessProjectId">
						<xn-page-select
							ref="xnbusinessProjectIdPageSelectRef"
							v-model:value="formData.businessProjectId"
							placeholder="请选择结算项目"
							allow-clear
							show-search
							:page-function="businessProjectIdSelector"
							:echo-function="businessProjectIdEcho"
							@change="settlementProjectChange"
							:disabled="formData.businessStatus==='1' || formData.businessStatus==='2'"
						/>
					</a-form-item>
				</a-col>
				<a-col :span="12" v-show="formData.businessProjectIdCode === '01' || formData.businessProjectIdCode === '02' || formData.businessProjectIdCode === '05' || formData.businessProjectIdCode === '06'">
					<a-form-item label="结算对象：" name="businessObjectId">
						<xn-page-select
							ref="xnbusinessObjectIdPageSelectRef"
							v-model:value="formData.businessObjectId"
							placeholder="请选择结算对象"
							allow-clear
							show-search
							:page-function="businessObjectIdSelector"
							:echo-function="businessObjectIdEcho"
							@change="settlementObjectChange"
							:disabled="formData.businessStatus==='1' || formData.businessStatus==='2'"
						/>
					</a-form-item>
				</a-col>
				<a-col :span="24" v-show="isSettlementByTicket">
					<a-form-item label="结算单据：" name="businessBusinessIds">
						<xn-page-select
							ref="xnbusinessBusinessIdsPageSelectRef"
							v-model:value="formData.businessBusinessIds"
							placeholder="请选择结算单据"
							allow-clear
							show-search
							:page-function="businessBusinessIdsSelector"
							:echo-function="businessBusinessIdsEcho"
							mode="multiple"
							@change="settlementBusinessChange"
							:disabled="formData.businessStatus==='1' || formData.businessStatus==='2'"
						/>
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="收（付）款账户：" name="businessAccountId">
						<xn-page-select
							ref="xnbusinessAccountIdPageSelectRef"
							v-model:value="formData.businessAccountId"
							placeholder="请选择收（付）款账户"
							allow-clear
							show-search
							:page-function="businessAccountIdSelector"
							:echo-function="businessAccountIdEcho"
							@change="settlementAccountChange"
							:disabled="formData.businessStatus==='1' || formData.businessStatus==='2'"
						/>
					</a-form-item>
				</a-col>

				<a-col :span="12">
					<a-form-item label="金额：" name="businessTotalAmount">
						<a-input v-model:value="formData.businessTotalAmount" placeholder="请输入金额" allow-clear
								 @change="countTotal"
								 :disabled="formData.businessStatus==='1' || formData.businessStatus === '2'"/>
					</a-form-item>
				</a-col>
				<a-col :span="12" v-show="formData.businessProjectIdCode === '01' || formData.businessProjectIdCode === '02' || formData.businessProjectIdCode === '05' || formData.businessProjectIdCode === '06'">
					<a-form-item label="折让金额：" name="businessDiscountAmount">
						<a-input v-model:value="formData.businessDiscountAmount" placeholder="请输入折让金额"
								 @change="countTotal"
								 :disabled="formData.businessStatus==='1' || formData.businessStatus==='2'"/>
					</a-form-item>
				</a-col>
				<a-col :span="12" v-show="formData.businessSettlementType==='0'">
					<a-form-item label="实收金额：" name="businessInAmount">
						<a-input v-model:value="formData.businessInAmount" placeholder="请输入实收金额" disabled
								 allow-clear/>
					</a-form-item>
				</a-col>
				<a-col :span="12" v-show="formData.businessSettlementType==='1'">
					<a-form-item label="实付金额：" name="businessPayAmount">
						<a-input v-model:value="formData.businessPayAmount" placeholder="请输入实付金额" disabled
								 allow-clear/>
					</a-form-item>
				</a-col>

<!--				<a-col :span="12">-->
<!--					<a-form-item label="结算前余额：" name="businessAccountBalanceBefore">-->
<!--						<a-input v-model:value="formData.businessAccountBalanceBefore" placeholder="请输入结算前余额"-->
<!--								 disabled-->
<!--								 allow-clear/>-->
<!--					</a-form-item>-->
<!--				</a-col>-->
<!--				<a-col :span="12">-->
<!--					<a-form-item label="结算后余额：" name="businessAccountBalanceAfter">-->
<!--						<a-input v-model:value="formData.businessAccountBalanceAfter" placeholder="请输入结算后余额"-->
<!--								 disabled-->
<!--								 allow-clear/>-->
<!--					</a-form-item>-->
<!--				</a-col>-->
				<!--				<a-col :span="12">-->
				<!--					<a-form-item label="经办人：" name="settlementHandlerId">-->
				<!--						<xn-page-select-->
				<!--							ref="xnsettlementHandlerIdPageSelectRef"-->
				<!--							v-model:value="formData.settlementHandlerId"-->
				<!--							placeholder="请选择经办人"-->
				<!--							allow-clear-->
				<!--							show-search-->
				<!--							:page-function="settlementHandlerIdSelector"-->
				<!--						/>-->
				<!--					</a-form-item>-->
				<!--				</a-col>-->
				<!--				<a-col :span="12">-->
				<!--					<a-form-item label="是否交班：" name="settlementHandover">-->
				<!--						<a-input v-model:value="formData.settlementHandover" placeholder="请输入是否交班" allow-clear/>-->
				<!--					</a-form-item>-->
				<!--				</a-col>-->
				<a-col :span="12">
					<a-form-item label="摘要：" name="businessAbstract">
						<a-input v-model:value="formData.businessAbstract" placeholder="请输入摘要" allow-clear
								 :disabled="formData.businessStatus==='1' || formData.businessStatus==='2'"/>
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="备注：" name="businessRemark">
						<a-input v-model:value="formData.businessRemark" placeholder="请输入备注" allow-clear
								 :disabled="formData.businessStatus==='1' || formData.businessStatus==='2'"/>
					</a-form-item>
				</a-col>
			</a-row>
		</a-form>
		<template #footer>
			<a-row :gutter="16">
				<a-col :span="9" align="end">
					<xn-page-select
						ref="xnreportIdPageSelectRef"
						v-model:value="reportId"
						placeholder="请选择打印格式"
						allow-clear
						show-search
						:page-function="reportIdSelector"
						:disabled="!formData.id"
					/>
				</a-col>
				<!--			<a-select-->
				<!--				v-model:value="reportId"-->
				<!--				:options="reportOptions"-->
				<!--				style="width: 40%"-->
				<!--				placeholder="请选择打印格式"-->
				<!--				:field-names="{-->
				<!--							label: 'reportName',-->
				<!--							value: 'id'-->
				<!--						}"-->
				<!--			>-->
				<!--			</a-select>-->
				<a-col :span="15" align="end">
					<a-button type="primary" @click="toPrint" :loading="submitLoading" style="margin-left: 5px"
							  :disabled="!formData.id">打印
					</a-button>
					<a-button type="primary" @click="onSubmit('0')" :loading="submitLoading" style="margin-left: 5px" v-show="formData.businessType==='JS'"
							  >
						{{formData.businessStatus === '2' ? '重新复用' : formData.businessStatus === '1' ? '反审核' : '保存草稿'}}
					</a-button>
					<a-button type="primary" @click="onSubmit('1')" :loading="submitLoading" style="margin-left: 5px"
							  danger :disabled="formData.businessStatus ==='2' || formData.businessStatus ==='1'">
						审核过账
					</a-button>
					<a-button type="default" @click="onSubmit('2')" :loading="submitLoading" style="margin-left: 5px"
							  danger :disabled="formData.businessStatus!=='0'">作废
					</a-button>
					<a-button type="primary" style="margin-left: 10px" @click="onClose">关闭</a-button>
				</a-col>
			</a-row>
		</template>
	</xn-form-container>
</template>

<script setup name="supplierSettlementForm">
import tool from '@/utils/tool'
import {cloneDeep} from 'lodash-es'
import {required} from '@/utils/formRules'
import {message} from 'ant-design-vue'
import {globalStore} from '@/store'
import supplierSettlementApi from '@/api/supplier/finance/supplierSettlementApi'
import supplierBusinessMainApi from "@/api/supplier/business/supplierBusinessMainApi";
import supplierSuppliersApi from "@/api/supplier/purchase/supplierSuppliersApi";
import supplierStoresApi from "@/api/supplier/base/supplierStoresApi";
import supplierAccountApi from "@/api/supplier/finance/supplierAccountApi";
import supplierSettlementProjectApi from "@/api/supplier/finance/supplierSettlementProjectApi";
import supplierUserApi from "@/api/supplier/structure/supplierUserApi";
import {ref} from "vue";
import publicReportsApi from "@/api/supplier/setting/publicReportsApi";
import homeCompanyApi from "@/api/home/setting/homeCompanyApi";
import supplierObjectsApi from "@/api/supplier/custom/supplierObjectsApi";
import nzh from "nzh"

const nzhcn = nzh.cn
// 抽屉状态
const open = ref(false)
const emit = defineEmits({successful: null})
const formRef = ref()
// 表单数据
const formData = ref({})
const submitLoading = ref(false)
const isSettlementByTicket = ref(false)
//打印报表
const reportId = ref()
const xnreportIdPageSelectRef = ref();
const reportIdSelector = (param) => {
	return publicReportsApi.publicReportsPage(param).then((data) => {
		if (data.records) {
			data.records = data.records.map((item) => {
				if (item.reportDefault === '1') {
					reportId.value = item.id
				}
				return {
					name: item.reportName,
					id: item.id,
				};
			});
		}
		return Promise.resolve(data)
	})
}
const printData = ref({})
//结算门店
const xnbusinessShippingStoreIdPageSelectRef = ref();
const businessShippingStoreIdSelector = (param) => {
	return supplierStoresApi.supplierStoresPage(param).then((data) => {
		if (data.records) {
			data.records = data.records.map((item) => {
				return {
					name: item.storeName,
					id: item.id,
				};
			});
		}
		return Promise.resolve(data)
	})
}
const businessShippingStoreIdEcho = (param) => {
	let echoList = []
	let echo = {}
	for (var i = 0; i < param.idList[0].length; i++) {
		echo.name = formData.value.businessShippingStoreIdName
		echo.id = param.idList[0][i]
		echoList.push(echo)
	}
	return Promise.resolve(echoList)
}
const settlementStoreChange = (value) => {
	if(value){
		businessIdsSearchParms.value.businessShippingStoreId = value
	}else{
		businessIdsSearchParms.value.businessShippingStoreId = null
	}
	formData.value.businessBusinessIds = []
	businessOption.value = []
	if(isSettlementByTicket.value){
		xnbusinessBusinessIdsPageSelectRef.value.onPage(businessIdsSearchParms.value);
	}
	countAmount()
}
//结算类型
const businessSettlementTypeOptions = ref([])
const businessSettlementTypeChange = (value) => {
	if (value.target.value) {
		formData.value.businessProjectId = null
		formData.value.businessObjectId = null
		formData.value.businessBusinessIds = []
		formData.value.businessTotalAmount = 0
		projectname.value = null
		xnbusinessProjectIdPageSelectRef.value.onPage({current: 1, projectType: value.target.value});
		formData.value.businessProjectIdCode = null //隐藏接单结算
		isSettlementByTicket.value = false  //取消按单结算
		countAmount()
	}
}
//按单结算
const showBusinessIds = (value) =>{
	if(value.target.checked){
		formData.value.businessBusinessIds = undefined
		xnbusinessBusinessIdsPageSelectRef.value.onPage(businessIdsSearchParms.value);
		formData.value.businessTotalAmount = 0
		countTotal()
	}else{
		settlementObjectChange(formData.value.businessObjectId) //取消按单结算，则按结算对象应收应付结算
	}

}
//结算项目
const projectOption = ref([])
const xnbusinessProjectIdPageSelectRef = ref();
const businessProjectIdSelector = (param) => {
	return supplierSettlementProjectApi.supplierSettlementProjectPage(param).then((data) => {
		if (data.records) {
			data.records = data.records.map((item) => {
				return {
					name: item.projectName,
					id: item.id,
					type: item.projectType,
					code: item.projectCode
				};
			});
		}
		projectOption.value = [...projectOption.value, ...data.records]
		return Promise.resolve(data)
	})
}
const businessProjectIdEcho = (param) => {
	let echoList = []
	let echo = {}
	for (var i = 0; i < param.idList[0].length; i++) {
		echo.name = formData.value.businessProjectIdName
		echo.id = param.idList[0][i]
		echoList.push(echo)
	}
	return Promise.resolve(echoList)
}
const projectname = ref() //结算项目名称
const projectCode = ref() //结算项目编码
const settlementProjectChange = (value) => {
	businessObjectData = []
	formData.value.businessObjectId = null
	formData.value.businessBusinessIds = null
	if (value) {
		let index = projectOption.value.findIndex(function (project) {
			return project.id === value
		})
		formData.value.businessSettlementType = projectOption.value[index].type
		projectname.value = projectOption.value[index].name
		formData.value.businessProjectIdCode = projectOption.value[index].code

		businessIdsSearchParms.value.businessType = null
		businessIdsSearchParms.value.businessStatus = "1"
		businessIdsSearchParms.value.BusinessPaymentStatus = "0,2"
		formData.value.businessBusinessIds = []
		formData.value.businessTotalAmount = 0
		formData.value.businessDiscountAmount = 0
		businessOption.value = []
		if (formData.value.businessProjectIdCode === '01') {
			businessIdsSearchParms.value.businessType = 'EN,RE'
			xnbusinessObjectIdPageSelectRef.value.onPage({current: 1, objectType: '0'})
		}else if (formData.value.businessProjectIdCode === '02') {
			businessIdsSearchParms.value.businessType = 'RS'
			xnbusinessObjectIdPageSelectRef.value.onPage({current: 1, objectType: '1'})
		}else if (formData.value.businessProjectIdCode === '05') {
			businessIdsSearchParms.value.businessType = 'SO,RS'
			xnbusinessObjectIdPageSelectRef.value.onPage({current: 1, objectType: '1'})
		}else if (formData.value.businessProjectIdCode === '06') {
			businessIdsSearchParms.value.businessType = 'RE'
			xnbusinessObjectIdPageSelectRef.value.onPage({current: 1, objectType: '0'})
		}else{
			xnbusinessObjectIdPageSelectRef.value.onPage({current: 1, objectType: null})
			formData.value.businessObjectId = null
			formData.value.businessBusinessIds = null
			isSettlementByTicket.value = false
		}
		if(isSettlementByTicket.value){
			xnbusinessBusinessIdsPageSelectRef.value.onPage(businessIdsSearchParms.value);
		}
	} else {
		projectname.value = null
		formData.value.businessProjectIdCode = null
		isSettlementByTicket.value = false
		// xnbusinessObjectIdPageSelectRef.value.onPage({current: 1, objectType: null})

	}
	countAmount()

}
//结算对象
const xnbusinessObjectIdPageSelectRef = ref();
let businessObjectData = []
const businessObjectIdSelector = (param) => {
	return supplierObjectsApi.supplierObjectsPage(param).then((data) => {
		if (data.records) {
			data.records = data.records.map((item) => {
				return {
					name: item.objectName,
					id: item.id,
					address: item.objectAddress,
					tel: item.objectTel,
					accountsPayable: item.objectAccountsPayable
				};
			});
		}
		businessObjectData = [...businessObjectData,...data.records]
		return Promise.resolve(data)
	})
}
const businessObjectIdEcho = (param) => {
	let echoList = []
	let echo = {}
	for (var i = 0; i < param.idList[0].length; i++) {
		echo.name = formData.value.businessObjectIdName
		echo.id = param.idList[0][i]
		echoList.push(echo)
	}
	return Promise.resolve(echoList)
}
const settlementObjectChange = (value) => {
	if(value){
		let index = businessObjectData.findIndex(function (businessObject){
			return businessObject.id === value
		})
		formData.value.businessObjectName = businessObjectData[index].name
		formData.value.businessObjectAddress = businessObjectData[index].address
		formData.value.businessObjectTel = businessObjectData[index].tel
		businessIdsSearchParms.value.businessObjectId = value
		formData.value.businessTotalAmount = businessObjectData[index].accountsPayable
	}else{
		formData.value.businessObjectName = null
		formData.value.businessObjectAddress = null
		formData.value.businessObjectTel = null
		businessIdsSearchParms.value.businessObjectId = null
		formData.value.businessTotalAmount = 0
	}
	formData.value.businessBusinessIds = []
	formData.value.businessDiscountAmount = 0
	businessOption.value = []
	xnbusinessBusinessIdsPageSelectRef.value.onPage(businessIdsSearchParms.value);
	countAmount()//计算汇总
}

const businessIdsSearchParms = ref({current: 1})
const businessOption = ref([])

const xnbusinessBusinessIdsPageSelectRef = ref();
const businessBusinessIdsSelector = (param) => {
	return supplierBusinessMainApi.supplierBusinessMainPendingPage(param).then((data) => {
		if (data.records) {
			data.records = data.records.map((item) => {
				return {
					name: item.businessCode + (formData.value.id ? "" : ("(未结金额：" + (item.businessSettlementType === '0' ? item.businessPendingInAmount : item.businessPendingPayAmount) + "元）")),
					id: item.id,
					amount: (formData.value.businessSettlementType==='0' ? (item.businessSettlementType === '0' ? item.businessPendingInAmount : (0-item.businessPendingPayAmount)) : (item.businessSettlementType === '1' ? item.businessPendingPayAmount : (0-item.businessPendingInAmount)))
				};
			});
		}
		businessOption.value = [...businessOption.value, ...data.records]
		return Promise.resolve(data)
	})
}
const businessBusinessIdsEcho = (param) => {
	let echoList = []
	let echo = {}
	for (var i = 0; i < param.idList[0].length; i++) {
		echo.name = formData.value.businessBusinessIdsName[i]
		echo.id = param.idList[0][i]
		echoList.push(echo)
	}
	return Promise.resolve(echoList)
}
const settlementBusinessChange = () => {
	countAmount()
}


const accountOption = ref([])
const xnbusinessAccountIdPageSelectRef = ref();
const businessAccountIdSelector = (param) => {
	return supplierAccountApi.supplierAccountPage(param).then((data) => {
		if (data.records) {
			data.records = data.records.map((item) => {
				return {
					name: item.accountName,
					id: item.id,
					balance: item.accountBalance
				};
			});
		}
		accountOption.value = [...accountOption.value, ...data.records]
		return Promise.resolve(data)
	})
}
const businessAccountIdEcho = (param) => {
	let echoList = []
	let echo = {}
	for (var i = 0; i < param.idList[0].length; i++) {
		echo.name = formData.value.businessAccountIdName
		echo.id = param.idList[0][i]
		echoList.push(echo)
	}
	return Promise.resolve(echoList)
}
const settlementAccountChange = (value) => {
	let index = accountOption.value.findIndex(function (account) {
		return account.id === value
	})
	// formData.value.businessAccountBalanceBefore = accountOption.value[index].balance
	// countAmount()
}

const countAmount = () => {
	//计算选中结算单据总金额
	if ((formData.value.businessProjectIdCode === '01' || formData.value.businessProjectIdCode === '02' || formData.value.businessProjectIdCode === '05' || formData.value.businessProjectIdCode === '06') && isSettlementByTicket.value) {
		let businessIds = formData.value.businessBusinessIds
		formData.value.businessTotalAmount = 0
		if (businessIds.length > 0) {
			for (var i = 0; i < businessIds.length; i++) {
				let index = businessOption.value.findIndex(function (business) {
					return business.id === businessIds[i]
				})
				formData.value.businessTotalAmount = formData.value.businessTotalAmount + businessOption.value[index].amount
			}
		}
	}
	countTotal()//计算汇总
}
const countTotal = () =>{
	//计算实收（付）金额
	if (formData.value.businessTotalAmount !== undefined && formData.value.businessDiscountAmount !== undefined) {
		if(formData.value.businessSettlementType==="0"){
			formData.value.businessInAmount = formData.value.businessTotalAmount - formData.value.businessDiscountAmount
		}else{
			formData.value.businessPayAmount = formData.value.businessTotalAmount - formData.value.businessDiscountAmount
		}
	} else {
		formData.value.businessPayAmount = 0
	}
	//构建自动摘要内容
	if (formData.value.businessSettlementType !== undefined) {
		let typeIndex = businessSettlementTypeOptions.value.findIndex(function (type) {
			return type.value === formData.value.businessSettlementType
		})
		formData.value.businessAbstract = businessSettlementTypeOptions.value[typeIndex].label
	}

	formData.value.businessAbstract = projectname.value === null ? '' : (formData.value.businessAbstract + (projectname.value) + (formData.value.businessSettlementType==='0' ? formData.value.businessInAmount : formData.value.businessPayAmount )+ '元')
}

// 打开抽屉
const onOpen = (record) => {
	open.value = true
	formData.value.businessDiscountAmount = 0
	formData.value.businessTotalAmount = 0
	formData.value.businessPayAmount = 0
	formData.value.businessType ='JS' //默认认为是新增结算单
	if (record) {
		let recordData = cloneDeep(record)
		formData.value = Object.assign({}, recordData)
		projectname.value = formData.value.businessProjectIdName
		// projectCode.value = formData.value.businessProjectIdCode

		//处理门店把出货门店和进货门店按情况挑出来
		formData.value.businessShippingStoreId = formData.value.businessSettlementType ==='0' ? formData.value.businessShippingStoreId : formData.value.businessStorageStoreId
		//如果不是结算单，而是销售或采购单生成的结算单
		if(formData.value.businessType!=='JS'){
			formData.value.businessBusinessIds = formData.value.id
		}
		//如果结算业务会涉及到业务单
		if (formData.value.businessProjectIdCode === '01' || formData.value.businessProjectIdCode === '02' || formData.value.businessProjectIdCode === '05' || formData.value.businessProjectIdCode === '06') {
			//查询结算对象
			nextTick(() =>{
				if(formData.value.businessProjectIdCode ==='01' || formData.value.businessProjectIdCode ==='06'){
					xnbusinessObjectIdPageSelectRef.value.onPage({current: 1, objectType: '0'})
				}
				if(formData.value.businessProjectIdCode ==='02' || formData.value.businessProjectIdCode ==='05'){
					xnbusinessObjectIdPageSelectRef.value.onPage({current: 1, objectType: '1'})
				}
			})
			//处理结算数据
			formData.value.businessBusinessIds = formData.value.businessBusinessIds === null ? null : formData.value.businessBusinessIds.split(',')
			formData.value.businessBusinessIdsName = formData.value.businessBusinessIdsName === null ? null : formData.value.businessBusinessIdsName.split(',')
			if(formData.value.businessBusinessIds){
				isSettlementByTicket.value = true
				if (formData.value.businessProjectIdCode === '01') {
					businessIdsSearchParms.value.businessType = 'EN,RE'
				}else if (formData.value.businessProjectIdCode === '02') {
					businessIdsSearchParms.value.businessType = 'RS'
				}else if (formData.value.businessProjectIdCode === '05') {
					businessIdsSearchParms.value.businessType = 'SO,RS'
				}else if (formData.value.businessProjectIdCode === '06') {
					businessIdsSearchParms.value.businessType = 'RE'
				}
				businessIdsSearchParms.value.businessShippingStoreId = formData.value.businessShippingStoreId
				businessIdsSearchParms.value.businessStatus = "1"
				// businessIdsSearchParms.value.BusinessPaymentStatus = "0,2"
				nextTick(() => {
					xnbusinessBusinessIdsPageSelectRef.value.onPage(businessIdsSearchParms.value);
				})
			}
		}

	}
	businessSettlementTypeOptions.value = tool.dictList('PROJECT_TYPE')
	nextTick(() => {
		xnbusinessProjectIdPageSelectRef.value.onPage({current: 1});
		xnbusinessShippingStoreIdPageSelectRef.value.onPage({current: 1});
		xnbusinessAccountIdPageSelectRef.value.onPage({current: 1});
		xnreportIdPageSelectRef.value.onPage({current: 1, reportType: 'SF'});
	});
}
// 关闭抽屉
const onClose = () => {
	formRef.value.resetFields()
	projectname.value = null
	formData.value = {}
	businessIdsSearchParms.value = {}
	projectOption.value = []
	formData.value.businessProjectIdCode = null
	isSettlementByTicket.value = false
	open.value = false

}
// 默认要校验的
const formRules = {
	businessShippingStoreId: [required('请选择结算门店')],
	businessSettlementType: [required('请选择结算业务类型')],
	businessProjectId: [required('请选择结算项目')],
	businessTotalAmount: [required('请输入金额')],
	businessPayAmount: [required('请输入实收（付）金额')],
	businessAccountId: [required('请选择结算账户')],

}
// 验证并提交数据
const onSubmit = (status) => {
	formRef.value
		.validate()
		.then(() => {
			submitLoading.value = true
			const formDataParam = cloneDeep(formData.value)
			if (formData.value.businessProjectIdCode === '01' || formData.value.businessProjectIdCode === '02' || formData.value.businessProjectIdCode === '05' || formData.value.businessProjectIdCode === '06') {
				if (formDataParam.businessObjectId === undefined) {
					message.warn('请选择结算对象')
					submitLoading.value = false
					return
				}
				if (isSettlementByTicket.value && formDataParam.businessBusinessIds === undefined) {
					message.warn('请选择结算单据')
					submitLoading.value = false
					return
				} else {
					formDataParam.businessBusinessIds = formDataParam.businessBusinessIds.join(',')
				}
			}
			if(formDataParam.businessTotalAmount*1<=0){
				message.warn('结算金额必须大于0')
				submitLoading.value = false
				return
			}
			formDataParam.businessStatus = status
			if(formDataParam.businessSettlementType==='1'){
				formDataParam.businessStorageStoreId = formData.value.businessShippingStoreId
				formDataParam.businessShippingStoreId = null

			}
			console.log(formDataParam)
			return	supplierSettlementApi.supplierSettlementSubmitForm(formDataParam, formDataParam.id)
				.then(() => {
					onClose()
					emit('successful')
				})
				.finally(() => {
					submitLoading.value = false
				})
		})
		.catch(() => {
		})
}
//获取基础信息
const getOwnerCompany = async () => {
	printData.value = formData.value
	console.log(printData.value)
	await homeCompanyApi.homeCompanyDetail({id: globalStore().userInfo.companyId}).then((res) => {
		printData.value.companyName = res.companyName
		printData.value.companyTel = res.companyTel
		printData.value.companyAddress = res.companyAddress
		printData.value.printDate = tool.parseTime(new Date(), '{y}-{m}-{d} {h}:{i}:{s}')
		printData.value.businessPayAmountUp = nzhcn.toMoney(printData.value.businessPayAmount)
		printData.value.businessDiscountAmount === 0 ? '0.00' : printData.value.businessDiscountAmount
	})
}
//打印
const toPrint = async () => {
	await getOwnerCompany()
	publicReportsApi.publicReportsDetail({id: reportId.value}).then((data) => {
		var hiprintTemplate = new hiprint.PrintTemplate({
			template: JSON.parse(Object.assign({}, data).reportContent)
		})
		hiprintTemplate.print(printData.value)
	})
}
// 抛出函数
defineExpose({
	onOpen
})
</script>
